/**
 * CommonUtil.java
 * xmlanalysis
 * 2011-8-23
 */
package com.lyjilu.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Properties;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.lyjilu.log.UtilException;

/**
 * @author liuyongjie 2011-8-23 ����03:53:25
 */
public class CommonUtil {
	public static void main(String[] args) {
		try {
			CommonUtil
					.loadProperties("/xmlanalysis/src/commonconfig.properties");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * ���������ļ�������Properties
	 * 
	 * @param fileName
	 *            �����ļ�·��
	 * @return �Ѿ��������ݵ����Զ���
	 * @throws IOException
	 *             ����ļ������ڣ������Լ��ش��󡣽��׳����쳣
	 */
	public static Properties loadProperties(String fileName) throws IOException {
		Properties pp = new Properties();
		InputStream in = null;
		try {
			in = new FileInputStream(fileName);
			pp.load(in);
			String charset = pp.getProperty("charset");
			if (charset != null) {
				for (Enumeration<?> e = pp.propertyNames(); e.hasMoreElements();) {
					String key = (String) e.nextElement();
					pp.setProperty(key, new String(pp.getProperty(key)
							.getBytes("ISO-8859-1"), charset));
				}
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (IOException e) {
					in = null;
				}
		}
		return pp;
	}

	/**
	 * ͨ��·������һ��XML�ļ�������Document����
	 * 
	 * @param path
	 *            �ļ�·����url·��
	 * @return XMLDocument����
	 * @throws DocumentException
	 *             ����ȡ�ĵ������׳�
	 */
	public static Document loadXMLFile(String path) throws DocumentException {
		Document doc = null;
		SAXReader xmlReader = new SAXReader();
		doc = xmlReader.read(new File(path));
		return doc;
	}

	/**
	 * ͨ��·������һ��XML�ļ�������Document����
	 * 
	 * @param url
	 *            url·��
	 * @return XMLDocument����
	 * @throws MalformedURLException
	 *             ���url�����׳�
	 * @throws DocumentException
	 *             ����ȡ�ĵ������׳�
	 */
	public static Document loadXMLURL(String url) throws MalformedURLException,
			DocumentException {
		Document doc = null;
		SAXReader xmlReader = new SAXReader();
		doc = xmlReader.read(new URL(url));
		return doc;
	}

	public static void saveXMLFile(Document doc, String path, String encoding)
			throws IOException {
		File file = new File(path);
		if (!file.getParentFile().exists())
			file.getParentFile().mkdirs();
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding(encoding);
		XMLWriter writer = null;
		try {
			writer = new XMLWriter(new FileWriter(path), format);
			writer.write(doc);
		} catch (IOException e) {
			throw e;
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}

	}
}
